﻿Imports VendasDAL
Imports System.Text.RegularExpressions
Public Class ClientesBLL
    Dim ObjDAL As New ClienteDAL
#Region "Propriedades"
    Property Codigo As Integer
    Property Nome As String
    Property Endereco As String
    Property Num As String
    Property Bairro As String
    Property Cidade As String
    Property Uf As String
    Property Complemento As String
    Property Cep As String
    Property Email As String
    Property Obs As String
    Property DtNascimento As Date
    Property DtCadastro As Date
    Property Status As Integer
    Property ccc_codigo As Integer
    Property tpe_codigo As Integer
    Property cpf As String
    Property rg As String
    Property cnpj As String
    Property inscricao As String
    Property telres As String
    Property telcom As String
    Property telcel As String
    Property telfax As String
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(codigo As Integer, nome As String, endereco As String, num As String, bairro As String, cidade As String, uf As String, complemento As String, cep As String,
            email As String, obs As String, dtnascimento As Date, status As Integer, tpecodigo As Integer, cpf As String, rg As String, cnpj As String, ie As String,
            telres As String, telcom As String, telcel As String, telfax As String)
        Me.Codigo = codigo
        Me.Nome = nome
        Me.Endereco = endereco
        Me.Num = num
        Me.Bairro = bairro
        Me.Cidade = cidade
        Me.Uf = uf
        Me.Complemento = complemento
        Me.Cep = cep
        Me.Email = email
        Me.Obs = obs
        Me.DtNascimento = dtnascimento
        Me.Status = status
        Me.tpe_codigo = tpecodigo
        Me.cpf = cpf
        Me.rg = rg
        Me.cnpj = cnpj
        Me.inscricao = ie
        Me.telres = telres
        Me.telcom = telcom
        Me.telcel = telcel
        Me.telfax = telfax


    End Sub
    Sub New(codigo)
        carrega(codigo)
    End Sub
#End Region

#Region "Inserir/alterar cliente"
    Public Function InserirAlterar()
        Try

            If (Me.Nome <> "" And Me.Endereco <> "" And Me.Bairro <> "" And Me.Cidade <> "" And Me.Num <> "" And Me.Uf <> "" And Me.Cep <> "") Then
                If (Cep.Length = 8) Then
                    If (ValidaCPF()) Then
                        If (ValidaCNPJ()) Then
                            If (ValidaEmail(Me.Email)) Then
                                If (ValidaIE(Me.Uf, Me.inscricao)) Then
                                    If (validar().Rows.Count = 0) Then
                                        ObjDAL = New ClienteDAL
                                        If (ObjDAL.ValidaExcluir(Me.Codigo, Me.Status)) Then
                                            Me.Codigo = ObjDAL.InserirAlterar(Me.Codigo, Me.Nome, Me.Endereco, Me.Num, Me.Bairro, Me.Cidade, Me.Uf, Me.Complemento, Me.Cep, Me.Email, Me.Obs, Me.DtNascimento, Me.Status, Me.tpe_codigo, Me.cpf, Me.rg, Me.cnpj, Me.inscricao, Me.telres, Me.telcom, Me.telcel, Me.telfax)
                                        Else : Throw New Exception("Este cliente não pode ser modificado, verifique antes a ocorrência do mesmo no(s) formulário(s): Placa")
                                        End If
                                    Else : Throw New Exception("Cliente já cadastrado")
                                    End If
                                Else : Throw New Exception("Inscrição estadual inválido")
                                End If
                            Else : Throw New Exception("Email inválido")
                            End If
                        Else : Throw New Exception("CNPJ inválido")
                        End If
                    Else : Throw New Exception("CPF inválido")
                    End If
                Else : Throw New Exception("CEP inválido")
                End If
            Else : Throw New Exception("Favor preencher os campos obrigatórios")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (Me.Codigo <= 0) Then
            Return False
        Else : Return True
        End If
    End Function
#End Region

#Region "Validações"
    Public Function ValidaCPF() As Boolean
        Dim i, a, n1, n2 As Integer
        If tpe_codigo = 2 Then
            Return True
        Else
            cpf = Me.cpf

            If CPF = "" OrElse _
              CPF.Trim.Length <> 11 OrElse _
              CPF = "11111111111" OrElse _
              CPF = "22222222222" OrElse _
              CPF = "33333333333" OrElse _
              CPF = "44444444444" OrElse _
              CPF = "55555555555" OrElse _
              CPF = "66666666666" OrElse _
              CPF = "77777777777" OrElse _
              CPF = "88888888888" OrElse _
              CPF = "99999999999" Then
                Return False
            End If

            For a = 0 To 1
                n1 = 0
                For i = 1 To 9 + a
                    n1 = n1 + Val(Mid(CPF, i, 1)) * (11 + a - i)
                Next
                n2 = 11 - (n1 - (Int(n1 / 11) * 11))
                If n2 = 10 Or n2 = 11 Then n2 = 0
                If n2 <> Val(Mid(CPF, 10 + a, 1)) Then
                    Return False
                End If
            Next
            Return True
        End If

    End Function
    Public Function ValidaCNPJ() As Boolean
        If tpe_codigo = 1 Then
            Return True
        Else
            Dim RecebeCNPJ As String
            Dim Numero(14) As Integer
            Dim Soma, Resultado1, Resultado2 As Integer
            cnpj = Me.cnpj
            RecebeCNPJ = CNPJ.Trim

            If RecebeCNPJ.Length <> 14 Or _
            RecebeCNPJ = "00000000000000" Or _
            RecebeCNPJ = "11111111111111" Or _
            RecebeCNPJ = "22222222222222" Or _
            RecebeCNPJ = "33333333333333" Or _
            RecebeCNPJ = "44444444444444" Or _
            RecebeCNPJ = "55555555555555" Or _
            RecebeCNPJ = "66666666666666" Or _
            RecebeCNPJ = "77777777777777" Or _
            RecebeCNPJ = "88888888888888" Or _
            RecebeCNPJ = "99999999999999" Then
                Return False
            Else
                Numero(1) = CInt(Mid(RecebeCNPJ, 1, 1))
                Numero(2) = CInt(Mid(RecebeCNPJ, 2, 1))
                Numero(3) = CInt(Mid(RecebeCNPJ, 3, 1))
                Numero(4) = CInt(Mid(RecebeCNPJ, 4, 1))
                Numero(5) = CInt(Mid(RecebeCNPJ, 5, 1))
                Numero(6) = CInt(Mid(RecebeCNPJ, 6, 1))
                Numero(7) = CInt(Mid(RecebeCNPJ, 7, 1))
                Numero(8) = CInt(Mid(RecebeCNPJ, 8, 1))
                Numero(9) = CInt(Mid(RecebeCNPJ, 9, 1))
                Numero(10) = CInt(Mid(RecebeCNPJ, 10, 1))
                Numero(11) = CInt(Mid(RecebeCNPJ, 11, 1))
                Numero(12) = CInt(Mid(RecebeCNPJ, 12, 1))
                Numero(13) = CInt(Mid(RecebeCNPJ, 13, 1))
                Numero(14) = CInt(Mid(RecebeCNPJ, 14, 1))



                Soma = Numero(1) * 5 + Numero(2) * 4 + Numero(3) * 3 + Numero(4) * 2 + Numero(5) * 9 + Numero(6) * 8 + Numero(7) * 7 + Numero(8) * 6 + Numero(9) * 5 + Numero(10) * 4 + Numero(11) * 3 + Numero(12) * 2
                Soma = Soma - (11 * (Int(Soma / 11)))



                If Soma = 0 Or Soma = 1 Then
                    Resultado1 = 0
                Else
                    Resultado1 = 11 - Soma
                End If

                If Resultado1 = Numero(13) Then
                    Soma = Numero(1) * 6 + Numero(2) * 5 + Numero(3) * 4 + Numero(4) * 3 + Numero(5) * 2 + Numero(6) * 9 + Numero(7) * 8 + Numero(8) * 7 + Numero(9) * 6 + Numero(10) * 5 + Numero(11) * 4 + Numero(12) * 3 + Numero(13) * 2
                    Soma = Soma - (11 * (Int(Soma / 11)))

                    If Soma = 0 Or Soma = 1 Then
                        Resultado2 = 0
                    Else
                        Resultado2 = 11 - Soma
                    End If

                    If Resultado2 = Numero(14) Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    Return False
                End If
            End If
        End If

    End Function
    Function ValidaEmail(ByVal email As String) As Boolean
        If email = "" Then
            Return True
        Else
            Dim strRegex As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + "\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + ".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
            Dim re As New Regex(strRegex)
            If (re.IsMatch(email)) Then
                Return True
            Else
                Return False
            End If
        End If
    End Function
    Public Function ValidaIE(ByVal pUF As String, ByVal pInscr As String)
        ValidaIE = False
        Dim strBase As String
        Dim strBase2 As String
        Dim strOrigem As String
        Dim strDigito1 As String
        Dim strDigito2 As String
        Dim intPos As Integer
        Dim intValor As Integer
        Dim intSoma As Integer
        Dim intResto As Integer
        Dim intNumero As Integer
        Dim intPeso As Integer
        Dim intDig As Integer

        strBase = ""
        strBase2 = ""
        strOrigem = ""
        If Trim(pInscr) = "" Or Trim(pInscr).ToUpper = "ISENTO" Then
            ValidaIE = True
            Exit Function
        End If
        For intPos = 1 To Len(Trim(pInscr))
            If InStr(1, "0123456789P", Mid$(pInscr, intPos, 1), vbTextCompare) > 0 Then
                strOrigem = strOrigem & Mid$(pInscr, intPos, 1)
            End If
        Next
        Select Case pUF
            Case "AC"    ' Acre
                strBase = Microsoft.VisualBasic.Left$(Trim(strOrigem) & "000000000", 9)
                If Microsoft.VisualBasic.Left(strBase, 2) = "01" And Mid$(strBase, 3, 2) <> "00" Then
                    intSoma = 0
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * (10 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(If(intResto < 2, "0", Str(11 - intResto)), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
            Case "AL"    ' Alagoas
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                If Microsoft.VisualBasic.Left(strBase, 2) = "24" Then
                    intSoma = 0
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * (10 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intSoma = intSoma * 10
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto = 10, "0", Str(intResto)), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
            Case "AM"    ' Amazonas
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                intSoma = 0
                For intPos = 1 To 8
                    intValor = Val(Mid$(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
                If intSoma < 11 Then
                    strDigito1 = Microsoft.VisualBasic.Right(Str(11 - intSoma), 1)
                Else
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                End If
                strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "AP"    ' Amapa
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                intPeso = 0
                intDig = 0
                If Microsoft.VisualBasic.Left(strBase, 2) = "03" Then
                    intNumero = Val(Microsoft.VisualBasic.Left(strBase, 8))
                    If intNumero >= 3000001 And _
                    intNumero <= 3017000 Then
                        intPeso = 5
                        intDig = 0
                    ElseIf intNumero >= 3017001 And _
                        intNumero <= 3019022 Then
                        intPeso = 9
                        intDig = 1
                    ElseIf intNumero >= 3019023 Then
                        intPeso = 0
                        intDig = 0
                    End If
                    intSoma = intPeso
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * (10 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 11
                    intValor = 11 - intResto
                    If intValor = 10 Then
                        intValor = 0
                    ElseIf intValor = 11 Then
                        intValor = intDig
                    End If
                    strDigito1 = Microsoft.VisualBasic.Right(Str(intValor), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
            Case "BA"    ' Bahia
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "00000000", 8)
                If InStr(1, "0123458", Microsoft.VisualBasic.Left(strBase, 1), vbTextCompare) > 0 Then
                    intSoma = 0
                    For intPos = 1 To 6
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * (8 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 10
                    strDigito2 = Microsoft.VisualBasic.Right(IIf(intResto = 0, "0", Str(10 - intResto)), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 6) & strDigito2
                    intSoma = 0
                    For intPos = 1 To 7
                        intValor = Val(Mid$(strBase2, intPos, 1))
                        intValor = intValor * (9 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 10
                    strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto = 0, "0", Str(10 - intResto)), 1)
                Else
                    intSoma = 0
                    For intPos = 1 To 6
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * (8 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 11
                    strDigito2 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 6) & strDigito2
                    intSoma = 0
                    For intPos = 1 To 7
                        intValor = Val(Mid$(strBase2, intPos, 1))
                        intValor = intValor * (9 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                End If
                strBase2 = Microsoft.VisualBasic.Left(strBase, 6) & strDigito1 & strDigito2
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "CE"    ' Ceara
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                intSoma = 0
                For intPos = 1 To 8
                    intValor = Val(Mid$(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
                intResto = intSoma Mod 11
                intValor = 11 - intResto
                If intValor > 9 Then
                    intValor = 0
                End If
                strDigito1 = Microsoft.VisualBasic.Right(Str(intValor), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "DF"    ' Distrito Federal
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "0000000000000", 13)
                If Microsoft.VisualBasic.Left(strBase, 3) = "073" Then
                    intSoma = 0
                    intPeso = 2
                    For intPos = 11 To 1 Step -1
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * intPeso
                        intSoma = intSoma + intValor
                        intPeso = intPeso + 1
                        If intPeso > 9 Then
                            intPeso = 2
                        End If
                    Next
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 11) & strDigito1
                    intSoma = 0
                    intPeso = 2
                    For intPos = 12 To 1 Step -1
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * intPeso
                        intSoma = intSoma + intValor
                        intPeso = intPeso + 1
                        If intPeso > 9 Then
                            intPeso = 2
                        End If
                    Next
                    intResto = intSoma Mod 11
                    strDigito2 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 12) & strDigito2
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
            Case "ES"    ' Espirito Santo
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                intSoma = 0
                For intPos = 1 To 8
                    intValor = Val(Mid$(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
                intResto = intSoma Mod 11
                strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "GO"    ' Goias
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                If InStr(1, "10,11,15", Microsoft.VisualBasic.Left(strBase, 2), vbTextCompare) > 0 Then
                    intSoma = 0
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * (10 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 11
                    If intResto = 0 Then
                        strDigito1 = "0"
                    ElseIf intResto = 1 Then
                        intNumero = Val(Microsoft.VisualBasic.Left(strBase, 8))
                        strDigito1 = Microsoft.VisualBasic.Right(IIf(intNumero >= 10103105 And intNumero <= 10119997, "1", "0"), 1)
                    Else
                        strDigito1 = Microsoft.VisualBasic.Right(Str(11 - intResto), 1)
                    End If
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
            Case "MA"    ' Maranhão
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                If Microsoft.VisualBasic.Left(strBase, 2) = "12" Then
                    intSoma = 0
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * (10 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
            Case "MT"    ' Mato Grosso
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "0000000000", 10)
                intSoma = 0
                intPeso = 2
                For intPos = 10 To 1 Step -1
                    intValor = Val(Mid$(strBase, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                    If intPeso > 9 Then
                        intPeso = 2
                    End If
                Next
                intResto = intSoma Mod 11
                strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 10) & strDigito1
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "MS"    ' Mato Grosso do Sul
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                If Microsoft.VisualBasic.Left(strBase, 2) = "28" Then
                    intSoma = 0
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * (10 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
            Case "MG"    ' Minas Gerais
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "0000000000000", 13)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 3) & "0" & Mid$(strBase, 4, 8)
                intNumero = 2
                For intPos = 1 To 12
                    intValor = Val(Mid$(strBase2, intPos, 1))
                    intNumero = IIf(intNumero = 2, 1, 2)
                    intValor = intValor * intNumero
                    If intValor > 9 Then
                        strDigito1 = Format(intValor, "00")
                        intValor = Val(Microsoft.VisualBasic.Left(strDigito1, 1)) + _
                                      Val(Microsoft.VisualBasic.Right(strDigito1, 1))
                    End If
                    intSoma = intSoma + intValor
                Next
                intValor = intSoma
                While Microsoft.VisualBasic.Right(Format(intValor, "000"), 1) <> "0"
                    intValor = intValor + 1
                End While
                strDigito1 = Microsoft.VisualBasic.Right(Format(intValor - intSoma, "00"), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 11) & strDigito1
                intSoma = 0
                intPeso = 2
                For intPos = 12 To 1 Step -1
                    intValor = Val(Mid$(strBase2, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                    If intPeso > 11 Then
                        intPeso = 2
                    End If
                Next
                intResto = intSoma Mod 11
                strDigito2 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                strBase2 = strBase2 & strDigito2
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "PA"    ' Para
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                If Microsoft.VisualBasic.Left(strBase, 2) = "15" Then
                    intSoma = 0
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * (10 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
            Case "PB"    ' Paraiba
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                intSoma = 0
                For intPos = 1 To 8
                    intValor = Val(Mid$(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
                intResto = intSoma Mod 11
                intValor = 11 - intResto
                If intValor > 9 Then
                    intValor = 0
                End If
                strDigito1 = Microsoft.VisualBasic.Right(Str(intValor), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "PE"    ' Pernambuco
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "00000000000000", 14)
                intSoma = 0
                intPeso = 2
                For intPos = 13 To 1 Step -1
                    intValor = Val(Mid$(strBase, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                    If intPeso > 9 Then
                        intPeso = 2
                    End If
                Next
                intResto = intSoma Mod 11
                intValor = 11 - intResto
                If intValor > 9 Then
                    intValor = intValor - 10
                End If
                strDigito1 = Microsoft.VisualBasic.Right(Str(intValor), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 13) & strDigito1
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "PI"    ' Piaui
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                intSoma = 0
                For intPos = 1 To 8
                    intValor = Val(Mid$(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
                intResto = intSoma Mod 11
                strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "PR"    ' Parana
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "0000000000", 10)
                intSoma = 0
                intPeso = 2
                For intPos = 8 To 1 Step -1
                    intValor = Val(Mid$(strBase, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                    If intPeso > 7 Then
                        intPeso = 2
                    End If
                Next
                intResto = intSoma Mod 11
                strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                intSoma = 0
                intPeso = 2
                For intPos = 9 To 1 Step -1
                    intValor = Val(Mid$(strBase2, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                    If intPeso > 7 Then
                        intPeso = 2
                    End If
                Next
                intResto = intSoma Mod 11
                strDigito2 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                strBase2 = strBase2 & strDigito2
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "RJ"    ' Rio de Janeiro
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "00000000", 8)
                intSoma = 0
                intPeso = 2
                For intPos = 7 To 1 Step -1
                    intValor = Val(Mid$(strBase, intPos, 1))
                    intValor = intValor * intPeso
                    intSoma = intSoma + intValor
                    intPeso = intPeso + 1
                    If intPeso > 7 Then
                        intPeso = 2
                    End If
                Next
                intResto = intSoma Mod 11
                strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 7) & strDigito1
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "RN"    ' Rio Grande do Norte
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                If Microsoft.VisualBasic.Left(strBase, 2) = "20" Then
                    intSoma = 0
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * (10 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intSoma = intSoma * 10
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto > 9, "0", Str(intResto)), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
            Case "RO"    ' Rondonia
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                strBase2 = Mid$(strBase, 4, 5)
                intSoma = 0
                For intPos = 1 To 5
                    intValor = Val(Mid$(strBase2, intPos, 1))
                    intValor = intValor * (7 - intPos)
                    intSoma = intSoma + intValor
                Next
                intResto = intSoma Mod 11
                intValor = 11 - intResto
                If intValor > 9 Then
                    intValor = intValor - 10
                End If
                strDigito1 = Microsoft.VisualBasic.Right(Str(intValor), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "RR"    ' Roraima
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                If Microsoft.VisualBasic.Left(strBase, 2) = "24" Then
                    intSoma = 0
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * (10 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 9
                    strDigito1 = Microsoft.VisualBasic.Right(Str(intResto), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
            Case "RS"    ' Rio Grande do Sul
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "0000000000", 10)
                intNumero = Val(Microsoft.VisualBasic.Left(strBase, 3))
                If intNumero > 0 And intNumero < 468 Then
                    intSoma = 0
                    intPeso = 2
                    For intPos = 9 To 1 Step -1
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * intPeso
                        intSoma = intSoma + intValor
                        intPeso = intPeso + 1
                        If intPeso > 9 Then
                            intPeso = 2
                        End If
                    Next
                    intResto = intSoma Mod 11
                    intValor = 11 - intResto
                    If intValor > 9 Then
                        intValor = 0
                    End If
                    strDigito1 = Microsoft.VisualBasic.Right(Str(intValor), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 9) & strDigito1
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
            Case "SC"    ' Santa Catarina
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                intSoma = 0
                For intPos = 1 To 8
                    intValor = Val(Mid$(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
                intResto = intSoma Mod 11
                strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "SE"    ' Sergipe
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000", 9)
                intSoma = 0
                For intPos = 1 To 8
                    intValor = Val(Mid$(strBase, intPos, 1))
                    intValor = intValor * (10 - intPos)
                    intSoma = intSoma + intValor
                Next
                intResto = intSoma Mod 11
                intValor = 11 - intResto
                If intValor > 9 Then
                    intValor = 0
                End If
                strDigito1 = Microsoft.VisualBasic.Right(Str(intValor), 1)
                strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "SP"    ' São Paulo
                If Microsoft.VisualBasic.Left(strOrigem, 1) = "P" Then
                    strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "0000000000000", 13)
                    strBase2 = Mid$(strBase, 2, 8)
                    intSoma = 0
                    intPeso = 1
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * intPeso
                        intSoma = intSoma + intValor
                        intPeso = intPeso + 1
                        If intPeso = 2 Then
                            intPeso = 3
                        End If
                        If intPeso = 9 Then
                            intPeso = 10
                        End If
                    Next
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(Str(intResto), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1 & Mid$(strBase, 11, 3)
                Else
                    strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "000000000000", 12)
                    intSoma = 0
                    intPeso = 1
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * intPeso
                        intSoma = intSoma + intValor
                        intPeso = intPeso + 1
                        If intPeso = 2 Then
                            intPeso = 3
                        End If
                        If intPeso = 9 Then
                            intPeso = 10
                        End If
                    Next
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(Str(intResto), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 8) & strDigito1 & Mid$(strBase, 10, 2)
                    intSoma = 0
                    intPeso = 2
                    For intPos = 11 To 1 Step -1
                        intValor = Val(Mid$(strBase, intPos, 1))
                        intValor = intValor * intPeso
                        intSoma = intSoma + intValor
                        intPeso = intPeso + 1
                        If intPeso > 10 Then
                            intPeso = 2
                        End If
                    Next
                    intResto = intSoma Mod 11
                    strDigito2 = Microsoft.VisualBasic.Right(Str(intResto), 1)
                    strBase2 = strBase2 & strDigito2
                End If
                If strBase2 = strOrigem Then
                    ValidaIE = True
                End If
            Case "TO"    ' Tocantins
                strBase = Microsoft.VisualBasic.Left(Trim(strOrigem) & "00000000000", 11)
                If InStr(1, "01,02,03,99", Mid$(strBase, 3, 2), vbTextCompare) > 0 Then
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 2) & Mid$(strBase, 5, 6)
                    intSoma = 0
                    For intPos = 1 To 8
                        intValor = Val(Mid$(strBase2, intPos, 1))
                        intValor = intValor * (10 - intPos)
                        intSoma = intSoma + intValor
                    Next
                    intResto = intSoma Mod 11
                    strDigito1 = Microsoft.VisualBasic.Right(IIf(intResto < 2, "0", Str(11 - intResto)), 1)
                    strBase2 = Microsoft.VisualBasic.Left(strBase, 10) & strDigito1
                    If strBase2 = strOrigem Then
                        ValidaIE = True
                    End If
                End If
        End Select
    End Function

#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            ObjDAL = New ClienteDAL
            ret = ObjDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function

#End Region

#Region "Funções"

#Region "Validar por replica"
    Private Function validar() As DataTable
        Dim ret As DataTable
        Try
            If tpe_codigo = 1 Then
                ret = objfuncoes.ValidaReplica(Me.cpf, Me.Codigo, "Vendas.Ofi_cliente", "cli_codigo", "cli_cpf")
            Else
                ret = objfuncoes.ValidaReplica(Me.cnpj, Me.Codigo, "Vendas.Ofi_cliente", "cli_codigo", "cli_cnpj")
            End If

        Catch ex As Exception
            Throw ex
        Finally

        End Try
        Return ret
    End Function
#End Region

#End Region

#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            ObjDAL = New ClienteDAL
            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.Codigo = Integer.Parse(tab.Rows(0)("cli_codigo").ToString)
                Me.Nome = tab.Rows(0)("cli_nome").ToString
                Me.Endereco = tab.Rows(0)("cli_end").ToString
                Me.Num = tab.Rows(0)("cli_numero").ToString
                Me.Bairro = tab.Rows(0)("cli_bairro").ToString
                Me.Cidade = tab.Rows(0)("cli_cidade").ToString
                Me.Uf = tab.Rows(0)("est_uf").ToString
                Me.Complemento = tab.Rows(0)("cli_complemento").ToString
                Me.Cep = tab.Rows(0)("cli_cep").ToString
                Me.Email = tab.Rows(0)("cli_email").ToString
                Me.Obs = tab.Rows(0)("cli_obs").ToString
                Me.DtNascimento = tab.Rows(0)("cli_dtnasc")
                '  Me.DtCadastro = tab.Rows(0)("cli_dtcadastro")
                Me.Status = Val(tab.Rows(0)("sta_codigo"))
                ' Me.ccc_codigo = (tab.Rows(0)("ccc_codigo"))
                Me.tpe_codigo = Val(tab.Rows(0)("tpe_codigo"))
                Me.cpf = (tab.Rows(0)("cli_cpf")).ToString
                Me.rg = (tab.Rows(0)("cli_rg")).ToString
                Me.cnpj = tab.Rows(0)("cli_cnpj").ToString
                Me.inscricao = (tab.Rows(0)("cli_inscricao")).ToString
                Me.telres = (tab.Rows(0)("cli_telres")).ToString
                Me.telcom = (tab.Rows(0)("cli_telcom")).ToString
                Me.telcel = (tab.Rows(0)("cli_telcel")).ToString
                Me.telfax = (tab.Rows(0)("cli_telfax")).ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

End Class
